package org.aplikator.client.widgets;

import java.io.Serializable;

import org.aplikator.client.Aplikator.AplikatorResources;
import org.aplikator.client.descriptor.PropertyDTO;

import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Label;

public class LabelFieldWidget<T extends Serializable> extends DataFieldBase<T> implements DataField<T> {
    private Label box = new Label();

    public LabelFieldWidget(String caption, PropertyDTO<T> property, boolean labelAbove, String width) {
        super(caption, property, false, labelAbove);
        add(box);
        box.addStyleName(PaneWidgetResources.INSTANCE.css().formData());

        if (property.getRefferedThrough() != null) {
            this.setEnabled(false);
        }
        if (width != null) {
            box.setWidth(width);
        } else {
            box.setWidth("" + Math.max(10, Math.floor(property.getSize() / 10) ) + "em");
        }
    }

    public void setValue(T value) {
        box.setText(value != null? value.toString():"");
    }

    public T getValue() {
        return null;
    }

    public HandlerRegistration addValueChangeHandler(@SuppressWarnings("rawtypes") ValueChangeHandler handler) {
        return null;//box.addValueChangeHandler(handler);
    }

    public void setEnabled(boolean enabled) {
    }

    public void setDirty(boolean dirty) {
        super.setDirty(dirty);
        if (dirty) {
            box.addStyleName(AplikatorResources.INSTANCE.css().dirty());
        } else {
            box.removeStyleName(AplikatorResources.INSTANCE.css().dirty());
        }
    }

}
